package com.progroup.tabAndroid4;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;
import android.widget.TextView;

import com.progroup.checks.Checks;
import com.progroup.checks.q;
import com.progroup.kyrsmoney.R;

public class CustomFragment extends Fragment {
	 private static final String KEY_CONTENT = "CustomFragment:List";
	 
	

	 public static CustomFragment newInstance(String content) {
		 CustomFragment fragment = new CustomFragment();
		 /*String date = "";
	    	DBAdapter db = new DBAdapter(Checks.context);
			
			db.open();
			Cursor cursor = db.kyrs_bank_tab(content);
			Cursor cursor2 = db.kyrs_bank_tab_yesterday(content);
			ArrayList<Object[]> list_bank_kyrs = new ArrayList<Object[]>();
			Log.i("CURSOR2", cursor2.getCount()+"");
			Log.i("CURSOR", cursor.getCount()+"");
	        
			if(cursor2!=null&cursor2.getCount()!=0){
				Log.i("CURSOR2", "TRUE");
				if (cursor != null&cursor.getCount()!=0) {
					Log.i("CURSOR", "TRUE");
					cursor.moveToFirst();
					cursor2.moveToFirst();
					//t1.name, t2.usd_buy, t2.usd_sale, t2.eur_buy, t2.eur_sale, t2.rub_buy, t2.rub_sale, 
					int i =0;
					do {
						list_bank_kyrs.add(new Object[]{
								cursor.getString(0), 
								cursor.getString(1), cursor2.getString(1), 
								cursor.getString(2), cursor2.getString(2),
								cursor.getString(4)});
						date = cursor.getString(3);
						Log.e("CURSOR2", "TRUE"+(i++));
						
					} while (cursor.moveToNext()|cursor2.moveToNext());
				}
			}else{
				Log.i("CURSOR2", "FALSE");
				if (cursor != null&cursor.getCount()!=0) {
					Log.i("CURSOR", "TRUE");
					cursor.moveToFirst();
					
				int i = 0;
					do {

						list_bank_kyrs.add(new Object[]{
								cursor.getString(0), 
								cursor.getString(1), "0", 
								cursor.getString(2), "0", 
								cursor.getString(4)});
						date = cursor.getString(3);
						
						Log.e("CURSOR2", "FALSE"+(i++));
						
					} while (cursor.moveToNext());
				}
			}
			
			
			
			cursor.close();
			cursor2.close();
			db.close();
	    	
	    	
			Log.i("CUSTOMFRAGMENT!!!!!!!!!!!!!!!!", content);
			fragment.list = list_bank_kyrs;
			fragment.content = content;
			fragment.date = date;*/
		 try{
		 fragment.list = q.list_bank_kyrs.get(content);
		 }catch (Exception e) {
			// TODO: handle exception
		}
		 fragment.content = content;
		 return fragment;
	 }
	 
	 private String mContent = "???";
	 private String content = "";
	 private String date = "";
	 private List<Object[]> list;
	 LinearLayout layoutcustom;
@Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);

	        //if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
	       //    mContent = savedInstanceState.getString(KEY_CONTENT);
	       // }
	    }
	 
	 @Override
	 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		 LinearLayout layout = new LinearLayout(getActivity());
		 layout.setBackgroundColor(Color.parseColor("#ffffff"));
		 layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		 layout.setGravity(Gravity.CENTER);
		 
		 
		 
		 ScrollView view = new ScrollView(getActivity());
		 view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		 view.setVerticalScrollBarEnabled(true);
		 view.setSoundEffectsEnabled(true);
		 
		 
		 LinearLayout layout1 =(LinearLayout)inflater.inflate(R.layout.custom_linearlayout, null);
		 
		 
		 layoutcustom =(LinearLayout)layout1.findViewById(R.id.custom_linearlayout_View);
		 
		 TextView text = (TextView)layout1.findViewById(R.id.custom_linearlayout_textview_date);
		 try{
		 text.setText(Checks.getKyrsDateText(getActivity(), list.get(0)[5].toString(), content));
		 }catch (Exception e) {
			// TODO: handle exception
		}
		 text.setWidth(Checks.dpw(310));
		 text.setTextSize(14);
		 text.setPadding(5, 5, 5, 5);
		 
		 LinearLayout layoutbackground =(LinearLayout)layout1.findViewById(R.id.custom_linearlayout_bacground);
		 LinearLayout layoutcustom1 =(LinearLayout)layout1.findViewById(R.id.custom_linearlayout_linearLayout1);
		 LinearLayout layoutcustom2 =(LinearLayout)layout1.findViewById(R.id.custom_linearlayout_linearLayout2);
		 LinearLayout layoutcustom3 =(LinearLayout)layout1.findViewById(R.id.custom_linearlayout_linearLayout3);
		 layoutbackground.setLayoutParams(new LinearLayout.LayoutParams(Checks.dpw(310), Checks.dpw(40)));
		 layoutcustom1.setLayoutParams(new LinearLayout.LayoutParams(Checks.dpw(160), Checks.dpw(40)));
		 layoutcustom2.setLayoutParams(new LinearLayout.LayoutParams(Checks.dpw(75), Checks.dpw(40)));
		 layoutcustom3.setLayoutParams(new LinearLayout.LayoutParams(Checks.dpw(75), Checks.dpw(40)));
		 LinearLayout layouttext =(LinearLayout)layout1.findViewById(R.id.custom_linearlayout_Text);
		 layouttext.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://sravnibank.com.ua/?utm_source=android&utm_medium=exchangeratei&utm_campaign=rates"));
				startActivity(browserIntent);
			}
		});
		 
		
		 layoutcustom3.setOnClickListener(new OnClickListener() {

			 public void onClick(View v) {
				 // TODO Auto-generated method stub
				 
				 //new ThreadTask().execute("2");
				 
				 addview("2");
				 
			 }
		 });
		 layoutcustom2.setOnClickListener(new OnClickListener() {
			 
			 public void onClick(View v) {
				 // TODO Auto-generated method stub
				 
				 //new ThreadTask().execute("1");
				 addview("1");
			 }
		 });
		 
		 layoutcustom1.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 //new ThreadTask().execute("0");
				 addview("0");
			}
		});
		 
		 
		 view.addView(layout1);
		 
		 layout.addView(view);
		 //new ThreadTask().execute("4");
		 addview("4");
		 
		 return layout;
	 }
	 
	 
	 public void addview(String r){
		 try{
		 if(r.equalsIgnoreCase("0")){
			 Collections.sort(list, new Comparator<Object[]>() {
				 
				 public int compare(Object[] lhs, Object[] rhs) {
					 // TODO Auto-generated method stub
					 String l = String.valueOf(lhs[0]);
					 String r = String.valueOf(rhs[0]);
					 return l.compareToIgnoreCase(r);
				 }
			 });
			 
		 }else if(r.equalsIgnoreCase("1")){
			 Collections.sort(list, new Comparator<Object[]>() {
				 
				 public int compare(Object[] lhs, Object[] rhs) {
					 // TODO Auto-generated method stub
					 double l = Double.parseDouble(lhs[1]+"");
					 double r = Double.parseDouble(rhs[1]+"");
					 if(l>r){return -1;}else if(l==r){return 0;}else if(l<r){return 1;}else{return 2;}
				 }
			 });
			 
		 }else if(r.equalsIgnoreCase("2")){
			 Collections.sort(list, new Comparator<Object[]>() {
				 
				 public int compare(Object[] lhs, Object[] rhs) {
					 // TODO Auto-generated method stub
					 double l = Double.parseDouble(lhs[3]+"");
					 double r = Double.parseDouble(rhs[3]+"");
					 if(l>r){return 1;}else if(l==r){return 0;}else if(l<r){return -1;}else{return 2;}
				 }
			 });
			 
		 }
		 
		 layoutcustom.removeAllViews();
		 int i=0;
		 
		 
		 do{
			 
		            
			// Log.i("CUSTOMVIEW!!!!", "__________________________________________________________________________________________________________");
			 CustomView vt = new CustomView(getActivity(), content);
			 layoutcustom.addView(vt.newView(
					 i, 
					 list.get(i)[0].toString(), 
					 list.get(i)[1].toString(), 
					 list.get(i)[2].toString(), 
					 list.get(i)[3].toString(), 
					 list.get(i)[4].toString(), 
					 list.get(i)[6].toString()));
			// Log.i("RAZNICA", "NAME="+list.get(i)[0].toString());
			// Log.i("RAZNICA", list.get(i)[1].toString().equalsIgnoreCase(list.get(i)[2].toString())+"");
			// Log.i("RAZNICA","BUY="+list.get(i)[1].toString() + "    SALE="+list.get(i)[3].toString()+"\n"+ "BUY_Y="+list.get(i)[2].toString() + "    SALE_Y="+list.get(i)[4].toString());
			// Log.i("CUSTOMVIEW!!!!", "__________________________________________________________________________________________________________");
		            
			 i++;
		 }while(i<list.size());
		 
		 }catch (Exception e) {
			// TODO: handle exception
		}
		 
		 
		 
	 }
	 
	 
	  @Override
	    public void onSaveInstanceState(Bundle outState) {
	        super.onSaveInstanceState(outState);
	    }
	 
 
	}